package com.happystreet.config
{
	import com.adobe.serialization.json.JSON;
	import com.happystreet.info.PartInfo;
	import com.happystreet.ui.ShopBox;

	/**
	 *商店配置->shops.xml 
	 * @author Administrator
	 * 
	 */	
	public class ShopsConfig
	{
		private static var shopsConfig:XML;
		private static var unlockLevels:Array;
		
		
		public static var buildShopList:Array;
		public static var shopsConfigList:Array;
		public static var rushTimes:Array;
		public function ShopsConfig()
		{
			throw new Error("ShopsConfig: this class can't be instantiated.");
		}
		
		/**
		 *获取购买全部货物时的打折 
		 * @return 
		 * 
		 */		
		public static function getBuyAllRate():Number
		{
			var rate:Number = Number(shopsConfig..params.@buyAllRate);
			return rate;
		}
		/**
		 *获取商店配置的商店个数 
		 * @return 
		 * 
		 */		
		public static function getShopCountLimit():uint
		{
			var shopList:XMLList = shopsConfig..shopType;
			return shopList.length();
		}
		/**
		 *获取商店的漂亮（装修）程度 
		 * @param shop_type		商店类型
		 * @param shop_level	商店等级
		 * @return 
		 * 
		 */		
		public static function getBeautyByTypeAndLevel(shop_type:int, shop_level:int):int
		{
			var resultValue:int;
			var beauty_string:String;
			var beauty_levels:Array;
			beauty_string = shopsConfig..shopType.(@type == shop_type).@beautyLevels;
			if (beauty_string != "")
			{
				beauty_levels = JSON.decode(shopsConfig..shopType.(@type == shop_type).@beautyLevels);
				resultValue = int(beauty_levels[shop_level]);
				return resultValue;
			}
			return 0;
		}
		
		/**
		 *根据商店类型以及商店规模等级获取漂亮等级列表 
		 * @param shop_type
		 * @param shop_size_level
		 * @return 
		 * 
		 */		
		public static function getShopBeautyLevels(shop_type:int, shop_size_level:int):Array
		{
			var beauty_levels:Array;
			var i:int;
			if (!shop_type)
			{
				return [];
			}
			beauty_levels = JSON.decode(shopsConfig..shopType.(@type == shop_type).@beautyLevels);
			beauty_levels.splice((shop_size_level + 1));
			i = 1;
			while (i < beauty_levels.length)
			{
				beauty_levels[i] = (int(beauty_levels[i]) + int(beauty_levels[(i - 1)]));
				i = (i + 1);
			}
			return beauty_levels;
		}
		/**
		 *通过商店类型获取升级列表
		 * @param shop_type
		 * @return 
		 * 
		 */		
		public static function getSizeUpgradeConfigByType(shop_type:int):Array
		{
			var sizeUpgradeList:Array;
			var matchingShop:XMLList;
			var costsList:Array;
			var timesList:Array;
			var first_level_config:Object;
			var i:int;
			var costsListString:String;
			var timesListString:String;
			var sizeUpgradeConfig:Object;
			sizeUpgradeList = new Array();
			matchingShop = shopsConfig..shopType.(@type == shop_type);
			if (matchingShop.length() == 1)
			{
				costsListString = matchingShop.@sizeUpgradeCost;
				costsList = costsListString.substring(1, (costsListString.length - 1)).split(",");
				timesListString = matchingShop.@sizeUpgradeTime;
				timesList = timesListString.substring(1, (timesListString.length - 1)).split(",");
			};
			first_level_config = {
				"sizeUpgradeCost":0,
				"sizeUpgradeTime":0
			};
			sizeUpgradeList.push(first_level_config);
			i = 0;
			while (i < costsList.length)
			{
				sizeUpgradeConfig = new Object();
				sizeUpgradeConfig.sizeUpgradeCost = costsList[i];
				sizeUpgradeConfig.sizeUpgradeTime = timesList[i];
				sizeUpgradeList.push(sizeUpgradeConfig);
				i = (i + 1);
			}
			return sizeUpgradeList;
		}
		
		/**
		 * 获取升级所需的持续时间
		 * @param shop_type
		 * @param size_upgrade_level
		 * @return 
		 * 
		 */		
		public static function getSizeUpgradeDuration(shop_type:int, size_upgrade_level:int):int
		{
			var timesListString:String;
			var timesList:Array;
			timesListString = shopsConfig..shopType.(@type == shop_type).@sizeUpgradeTime;
			timesList = timesListString.substring(1, (timesListString.length - 1)).split(",");
			return int(timesList[size_upgrade_level]);
		}
		
		/**
		 * 多少级可以开放新商店
		 * @param level
		 * @return 
		 * 
		 */		
		public static function getLevelForNewLocation(level:int):int
		{
			if (!unlockLevels)
			{
				unlockLevels = JSON.decode(shopsConfig..params.@levelsForNewLocation);
			}
			return unlockLevels[level];
		}

		
		/**
		 *设置商店配置 
		 * @param data
		 * 
		 */		
		public static function setShopsConfig(data:String):void
		{
			buildShopList = new Array();
			shopsConfigList = new Array();
			shopsConfig = new XML(data);
		}
		
		public static function getCashPriceForAction(ation:int):int
		{
			var time:int;
			var costCost:Array = JSON.decode(shopsConfig..params.@sizeActionCostCost);
			var costTime:Array = JSON.decode(shopsConfig..params.@sizeActionCostTime);
			var index:int;
			for each (time in costTime)
			{
				if (time > ation)
				{
					return (costCost[Math.max(0, (index - 1))]);
				}
				index++;
			}
			return costCost[(costCost.length - 1)];
		}
		/**
		 *创建商店 
		 * 
		 */		
		public static function createShopLists():void
		{
			var shop:XML;
			var shopBox:ShopBox;
			var configObj:Object;
			var idIndex:int;
			var shopsList:XMLList = shopsConfig..shopType;
			for each (shop in shopsList)
			{
				shopBox = new ShopBox();
				shopBox.name = shop.@name;
				shopBox.boxId = idIndex;
				shopBox.type = shop.@type;
				shopBox.symbol = shop.@symbol;
				shopBox.level = shop.@level;
				shopBox.costCoins = shop.@costCoins;
				shopBox.buildTime = shop.@buildTime;
				shopBox.difficulty = shop.@difficulty;
				shopBox.scale = 0.9;
				buildShopList.push(shopBox);
				idIndex++;
				configObj = new Object();
				configObj.name = shopBox.name;
				configObj.type = shopBox.type;
				configObj.symbol = shopBox.symbol;
				configObj.level = shopBox.level;
				configObj.costCoins = shopBox.costCoins;
				configObj.buildTime = shopBox.buildTime;
				configObj.difficulty = shopBox.difficulty;
				configObj.deltaRush = [4, 4, 4, 4, 4, 4];
				configObj.maxRush = JSON.decode(shop.@rushLevels);
				configObj.maxRushBonus = int(shop.@rushMaxBonus);
				shopsConfigList.push(configObj);
			}
			shopsConfigList.sortOn("type", Array.NUMERIC);
		}
		/**
		 *根据商店类型获取解锁等级 
		 * @param shop_type
		 * @return 
		 * 
		 */		
		public static function getShopUnlockLevelByType(shop_type:int):int
		{
			return int(shopsConfig..shopType.(@type == shop_type).@level);
		}
		
		/**
		 *根据商店类型获取商店名称 
		 * @param shop_type
		 * @return 
		 * 
		 */		
		public static function getShopNameByType(shop_type:int):String
		{
			return shopsConfig..shopType.(@type == shop_type).@name;
		}
		/**
		 *获取商店列表 
		 * @return 
		 * 
		 */		
		public static function getShopList():Array
		{
			if (buildShopList.length == 0)
			{
				createShopLists();
			}
			return buildShopList;
		}
		
		public static function getRushTimeByShopLevel(level:int):int
		{
			if (!rushTimes)
			{
				rushTimes = JSON.decode(shopsConfig..params.@rushTimeByShopLevel);
			}
			return rushTimes[level];
		}
		
		public static function getRushMaxBonusByShopTypeAndRushTime(type:int, time:int):int
		{
			var config:Object;
			for each (config in shopsConfigList)
			{
				if (config.type == type)
				{
					return config.maxRushBonus * time;
				}
			}
			return 0;
		}
		
		public static function getShopMaxRushByTypeAndLevel(type:int, level:int):int
		{
			var shop:Object;
			for each (shop in getShopsConfigs())
			{
				if (shop.type == type)
				{
					return (shop.maxRush[level]);
				}
			}
			return 0;
		}
		
		public static function getShopCostByType(shop_type:int):int
		{
			return int(shopsConfig..shopType.(@type == shop_type).@costCoins);
		}
		
		/**
		 *等待翻译完全 
		 * @param barInitValue
		 * @param barTotalLength
		 * @param type
		 * @param levelSize
		 * @return 
		 * 
		 */		
		public static function getBeautyBarConfig(barInitValue:int, barTotalLength:int, type:int, levelSize:int):Array
		{
			var levels:Array = getShopBeautyLevels(type, levelSize);
			var level:int;
			var nextLevel:int = levels[0];
			var index:int;
			while (barInitValue >= levels[index])
			{
				level = levels[index];
				nextLevel = levels[(index + 1)];
				index++;
			}
			var _local9:Number = barTotalLength / (levelSize + 1);
			var _local10:Number = _local9 * index;
			var _local11:Number = nextLevel>0 ? (barInitValue - level) / (nextLevel - level) : 0;
			var _local12:Number = _local10 + _local9 * _local11;
			return [_local12, index];
		}
		
		public static function getShopSymbolByType(shop_type:int):String
		{
			return String(shopsConfig..shopType.(@type == shop_type).@symbol);
		}
		
		public static function getPartInfoListByLevel(level:int):Array
		{
			var tempList:Array;
			var partInfo:PartInfo;
			var idIndex:uint;
			var countNeed:uint = 1;
			var deliveryUpgrade:String = shopsConfig..params.@deliveryUpgradeParts;
			var uri:String = decodeURIComponent(deliveryUpgrade);
			var deliveryUpgradeList:Array = JSON.decode(uri);
			var leveList:Array = deliveryUpgradeList[(level - 1)];
			var infoList:Array = new Array();
			for each (tempList in leveList)
			{
				partInfo = new PartInfo(tempList[idIndex], tempList[countNeed]);
				infoList.push(partInfo);
			}
			return infoList;
		}
		
		public static function getShopsConfigs():Array
		{
			if (shopsConfigList.length == 0)
			{
				createShopLists();
			}
			return shopsConfigList;
		}
		
		public static function getDeliveryUpgradeCostsByShopType(shop_type:int):Array
		{
			var upgrade_costs_list:Array;
			var matching_shop:XMLList;
			upgrade_costs_list = new Array();
			matching_shop = shopsConfig..shopType.(@type == shop_type);
			if (matching_shop.length() == 1)
			{
				upgrade_costs_list = JSON.decode(matching_shop.@deliveryUpgrade);
			}
			return upgrade_costs_list;
		}
		
		public static function getBuildDurationByType(shop_type:int):int
		{
			return int(shopsConfig..shopType.(@type == shop_type).@buildTime);
		}
		
		public static function getShopDefaultLayout(shop_type:int):Object
		{
			var defaultShop:String;
			defaultShop = (((('{"type":' + shop_type) + ',"level":0,"content":') + shopsConfig..shopType.(@type == shop_type).@defaultLayout) + "}");
			return JSON.decode(defaultShop);
		}
	}
}